/**************************************************************************
 *         Copyright(c) 2007 by Sunplus mMedia  Technology Co., Ltd.       *
 *                                                                         *
 *  This software is copyrighted by and is the property of Sunplus mMedia  *
 *  Technology Co., Ltd. All rights are reserved by Sunplus mMedia Techno- *
 *  logy Co., Ltd. This software may only be used in accordance with the   *
 *  corresponding license agreement. Any unauthorized use, duplication,    *
 *  distribution, or disclosure of this software is expressly forbidden.   *
 *                                                                         *
 *  This Copyright notice "M U S T" not be removed or modified without     *
 *  prior written consent of Sunplus mMedia Technology  Co., Ltd.          *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd. reserves the right to modify this  *
 *  software without notice.                                               *
 *                                                                         *
 *  Sunplus mMedia Technology Co., Ltd.                                    *
 *  19-1, Innovation First Road, Science-Based Industrial Park,            *
 *  Hsin-Chu, Taiwan, R.O.C.                                               *
 *                                                                         *
 **************************************************************************/
#include "general.h" 
#include "timer.h"
#include "os_api.h"
#include "app_main.h"
#include "os_msg.h"
#include "app_still_view.h"
#include "app_video_view.h"
#include "app_audio_rec.h"
#include "app_still_play.h"
#include "app_multi_play.h"
#include "app_video_play.h"
#include "app_audio_play.h"
#include "app_slide_show.h"
#include "app_pan_show.h"
#include "app_menu.h"
#include "app_cal_menu.h"
#include "app_usb.h"
#include "app_ui_power.h"//jintao.liu 2008-2-23
#include "sp1k_aud_api.h"
#include "sp1k_video_api.h"
#include "sp1k_osd_api.h"
#include "app_power_on.h"
#include "key_scan.h"
#include "app_osd_icon.h"
#include "app_ui_osd.h"
#include "smcfunc.h" //add for #36934
/**************************************************************************
 *                           C O N S T A N T S                            *
 **************************************************************************/
const UINT8 xdata prjDbgMode = 0;
/**************************************************************************
 *                              M A C R O S                               *
 **************************************************************************/

/**************************************************************************
 *                          D A T A    T Y P E S                          *
 **************************************************************************/

/**************************************************************************
 *                         G L O B A L    D A T A                         *
 **************************************************************************/
static appState_t code stStateFunc[] = {
	{ appPowerOn,	SP1K_BANK_POWER_ON },
	{ appStillView, 	SP1K_BANK_STILL_VIEW }, 
	{ appVideoView, 	SP1K_BANK_VIDEO_VIEW }, 
	{ appAudioRec,	SP1K_BANK_AUDIO_REC }, 
	{ appStillPlay, 	SP1K_BANK_STILL_PLAY }, 
	{ appVideoPlay, 	SP1K_BANK_VIDEO_PLAY }, 
	{ appAudioPlay, 	SP1K_BANK_AUDIO_PLAY }, 
	{ appMultiPlay,	SP1K_BANK_MUTI_PLAY }, 
	{ appUsb,		SP1K_BANK_USB }, 
	{ appMenu,		SP1K_BANK_MENU }, 
	{ appSlideShow,	SP1K_BANK_SLIDE_SHOW }, 
	{ appPanShow,	SP1K_BANK_PAN_SHOW }, 
	{ appCalMenu,	SP1K_BANK_MENU }, 
};
appState_t* xdata pActiveState = NULL;
CamState_t xdata stStateCtrl;
//appStateList_t xdata guiMode;

/**************************************************************************
 *                 E X T E R N A L    R E F E R E N C E S                 *
 **************************************************************************/
extern UINT8 xdata G_SDCardPlug;
extern UINT8 xdata G_ucStorageType;

/**************************************************************************
 *               F U N C T I O N    D E C L A R A T I O N S               *
 **************************************************************************/
static UINT8 appComMsgParsing(UINT8 msg);
static UINT8 appStateDestoryProc(UINT8 state);
static UINT8 appStateSwitch(UINT8 msg);

//-----------------------------------------------------------------------------
//appMain
//-----------------------------------------------------------------------------
/**
 * @brief		appMain
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/
void appMain (void)
{
	UINT8 msg;
	//printf("zhenglei....app main begin\n");
	osMsgAccept(&msg);
	printf("Msg(%bx)\n", msg);//
	//CPrintf("Phil: msg is %bu", msg);

	/*Check if there is a state switch*/
	
	if (msg > SP1K_MSG_STATE_MAX) {
		sp1kLED_BusySet(0);
		appComMsgParsing(msg);
		sp1kLED_BusySet(1);
		return;
	}

	printf("zhenglei....state change..0\n");
	sp1kBtnDisableAll();
	printf("zhenglei....state change..1\n");
	if (appStateDestoryProc(stStateCtrl.ActiveState) != SUCCESS) {
		printf("zhenglei....state change..2\n");
		//Task still not destoried
		sp1kBtnEnableAll();
		printf("zhenglei....state change..3\n");
		return;
	}

	/*state switch*/
	sp1kLED_BusySet(0);
	printf("zhenglei....state change..4\n");
	appStateSwitch(msg);
	printf("zhenglei....state change..5\n");
	sp1kLED_BusySet(1);
	printf("zhenglei....state change..6\n");
	sp1kBtnEnableAll();
	printf("zhenglei....state change..7\n");
}

//-----------------------------------------------------------------------------
//appComMsgParsing
//-----------------------------------------------------------------------------
/**
 * @brief		common msg parsing
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/

static UINT8 appComMsgParsing(UINT8 msg)
{
	devAttr_t* devAttr;
	
	if (pActiveState == NULL) {
		return FAIL;
	}

	if (msg < SP1K_MSG_KEY_PRESS_MAX) {
		//Button Pressed action
		appVoicePlay(msg,VOICE_TYPE_BEEP, VOICE_PLAYLEVLE_LOW);
	}
	
	if (msg < SP1K_MSG_KEY_MAX) {
		//add for mantis #36934@start
		if((appSDPlugStatusGet()==0) && (sp1kDiskTypeGet()==DEV_TYPE_DISK_SPI))
		{

			if(msg != SP1K_MSG_KEY_PRESS_POWER )
			{
				return FAIL;
			}

		}		
		//add for mantis #36934@end
		
		//Button action
		
		//jintao.liu 2008-2-23
		//add for auto power off
		appAutoOffTimeReload();
		//if (G_SDCardPlug || cardError) {
		devAttr = sp1kDevHdlGet();
		if (G_ucStorageType == K_MEDIA_SDF) {
			devAttr += DEV_ID_CARD;
		} else {
			devAttr += DEV_ID_DISK;
		}
		if (devAttr->devSts == DEV_STS_ERR){
			//card error
			if (msg != SP1K_MSG_KEY_PRESS_POWER && msg != SP1K_MSG_POWER_ON &&\
				msg != SP1K_MSG_KEY_PRESS_UP	&& msg != SP1K_MSG_KEY_PRESS_DOWN &&\
				msg != SP1K_MSG_KEY_PRESS_OK
				) {
				printf("zhenglei...card error\n");
				return FAIL;//					
			}
		}
	}
	
	/*Send msg to active state*/
	pActiveState->StateFunc(msg);		
	
	return SUCCESS;
}

//-----------------------------------------------------------------------------
//appStateDestoryProc
//-----------------------------------------------------------------------------
/**
 * @brief		State destory process
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/
static UINT8 appStateDestoryProc(UINT8 state)
{

	switch(state) {
		case APP_STATE_POWER_ON:
			sp1kBtnEnableAll();
			break;
			
		case APP_STATE_STILL_VIEW:
			sp1kOsdClear(ID_ICON_NONE, 0);
#ifdef _ENABLE_LCD_OFF_
			appOsdModeSw(0);
#endif /*_ENABLE_LCD_OFF_*/
			break;
			
		case APP_STATE_VIDEO_VIEW:
			if (sp1kVideoRecStatusGet() != VID_REC_IDLE) {
				//Retry to stop the video record
				osMsgPost(SP1K_MSG_MEDIA_REC_STOP);
				//CPrintf("Retry Stop");
				return FAIL;
			}
			sp1kOsdClear(ID_ICON_NONE, 0);
#ifdef _ENABLE_LCD_OFF_
			appOsdModeSw(0);
#endif /*_ENABLE_LCD_OFF_*/
			break;
			
		case APP_STATE_AUDIO_REC:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;
			
		case APP_STATE_STILL_PLAY:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;
			
		case APP_STATE_MUTI_PLAY:
			sp1kOsdClear(ID_ICON_NONE, 0);
			sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
			break;
			
		case APP_STATE_VIDEO_PLAY:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;
			
		case APP_STATE_AUDIO_PLAY:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;
			
		case APP_STATE_MENU:
			sp1kOsdClear(ID_ICON_NONE, 0);
			sp1kOsdSelWinSet(-1, -1, -1, -1, 0, -1);
			break;

		case APP_STATE_USB:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;

		case APP_STATE_SLIDE_SHOW:
			sp1kOsdClear(ID_ICON_NONE, 0);
			appAutoOffEn(1);
			break;
			
		case APP_STATE_PAN_SHOW:
			sp1kOsdClear(ID_ICON_NONE, 0);
			break;
			
		case APP_STATE_NULL:
		case APP_STATE_UNKNOWN:
		default:
			break;
	}
	return SUCCESS;
}

//-----------------------------------------------------------------------------
//appStateSwitch
//-----------------------------------------------------------------------------
/**
 * @brief		State switch process
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/
static UINT8 appStateSwitch(UINT8 msg)
{
	UINT8 nextState;
	UINT8 nextStateId;
	
	switch (msg) {
	case SP1K_MSG_STATE_INIT:
		pActiveState->StateFunc(SP1K_MSG_STATE_INIT);	
		return SUCCESS;
		
    	case SP1K_MSG_STATE_TO_POWER_ON:		
			printf("zhenglei....power on\n");
		nextState = APP_STATE_POWER_ON;
		nextStateId = STATE_FUNC_ID_POWER_ON;
		break;
		
    	case SP1K_MSG_STATE_TO_STILL_VIEW:		
		nextState = APP_STATE_STILL_VIEW;
		nextStateId = STATE_FUNC_ID_STILL_VIEW;
		break;
		
    	case SP1K_MSG_STATE_TO_VIDEO_VIEW:
		nextState = APP_STATE_VIDEO_VIEW;
		nextStateId = STATE_FUNC_ID_VIDEO_VIEW;
		break;
		
    	case SP1K_MSG_STATE_TO_AUDIO_REC:
		nextState = APP_STATE_AUDIO_REC;
		nextStateId = STATE_FUNC_ID_AUDIO_REC;
		break;
		
    	case SP1K_MSG_STATE_TO_STILL_PLAY:
		nextState = APP_STATE_STILL_PLAY;
		nextStateId = STATE_FUNC_ID_STILL_PLAY;
		break;
		
    	case SP1K_MSG_STATE_TO_MULTI_PLAY:
		nextState = APP_STATE_MUTI_PLAY;
		nextStateId = STATE_FUNC_ID_MUTI_PLAY;
		break;
		
    	case SP1K_MSG_STATE_TO_VIDEO_PLAY:
		nextState = APP_STATE_VIDEO_PLAY;
		nextStateId = STATE_FUNC_ID_VIDEO_PLAY;
		break;
		
    	case SP1K_MSG_STATE_TO_AUDIO_PLAY:
		nextState = APP_STATE_AUDIO_PLAY;
		nextStateId = STATE_FUNC_ID_AUDIO_PLAY;
		break;
		
    	case SP1K_MSG_STATE_TO_MENU:
		nextState = APP_STATE_MENU;
		nextStateId = STATE_FUNC_ID_MENU;
		break;
		
    	case SP1K_MSG_STATE_TO_USB:
		nextState = APP_STATE_USB;
		nextStateId = STATE_FUNC_ID_USB;
		break;
		
    	case SP1K_MSG_STATE_TO_SLIDE_SHOW:
		nextState = APP_STATE_SLIDE_SHOW;
		nextStateId = STATE_FUNC_ID_SLIDE_SHOW;
		break;

    	case SP1K_MSG_STATE_TO_PAN_SHOW:
		nextState = APP_STATE_PAN_SHOW;
		nextStateId = STATE_FUNC_ID_PAN_SHOW;
		break;
/*		
    	case SP1K_MSG_STATE_TO_MSGBOX:
		nextState = APP_STATE_MSG_BOX;
		nextStateId = STATE_FUNC_ID_MSG_BOX;
		break;
*/		
		case SP1K_MSG_STATE_TO_CAL_MENU:
		nextState = APP_STATE_CAL_MENU;
		nextStateId = STATE_FUNC_ID_CAL_MENU;
		break;

    	default:
		nextState = stStateCtrl.ActiveState;
		nextStateId = 0;
		//printf("msg is null\n");
		break;
	}

	if (stStateCtrl.ActiveState != nextState) {
		stStateCtrl.PrevState[1] = stStateCtrl.PrevState[0];
		stStateCtrl.PrevState[0] = stStateCtrl.ActiveState;
		stStateCtrl.ActiveState = nextState;	
		printf("zhenglei.....state...0\n");
		pActiveState = &stStateFunc[nextStateId];
		printf("zhenglei.....state...1\n");
		ACTIVE_FUNC_CLR();
		printf("zhenglei.....state...2\n");
	}
	
	if (msg != SP1K_MSG_NULL) {
		printf("zhenglei.....switch bank...0\n");
		switchbank(pActiveState->FuncBank);
		printf("zhenglei.....switch bank...1\n");
		#if 1//zhenglei test
		{
			//UINT8 i;
			//i=SP1K_MSG_STATE_INIT;
			pActiveState->StateFunc(0x01);	
		}
		#endif
		//pActiveState->StateFunc(SP1K_MSG_STATE_INIT);	
		printf("zhenglei.....switch bank...2\n");
	}

	printf("zhenglei...return sucess\n");
	
	return SUCCESS;
}

//-----------------------------------------------------------------------------
//appPreStateGet
//-----------------------------------------------------------------------------
/**
 * @brief		Get previous state
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
 
*/
UINT8 appPreStateGet(UINT8 PreLev)
{
	if (PreLev >= APP_STATE_BUFF_SIZE) {
		return APP_STATE_UNKNOWN;
	}
	
	return (stStateCtrl.PrevState[PreLev]);
}

//-----------------------------------------------------------------------------
//appCurrStateGet
//-----------------------------------------------------------------------------
/**
 * @brief		Get current state
 * @param	
 * @retval	NULL
 * @see
 * @author	Phil Lin
 * @since		2008-02-20
 * @todo		Something need to be done.
 * @bug		a bug need to be resolved.
*/
UINT8 appCurrStateGet(void)
{
	return (stStateCtrl.ActiveState);
	//return APP_STATE_STILL_VIEW;
}

